package com.xiaoming.freshmenapp.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.xiaoming.freshmenapp.R;
import com.xiaoming.freshmenapp.bean.MyBean;

import org.json.JSONException;
import org.json.JSONObject;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView home_back;
    private TextView home_title;
    private EditText rg_et_username;
    private EditText rg_et_name;
    private EditText rg_et_emli;
    private EditText rg_et_plone;
    private Spinner sp_department;
    private Spinner sp_major;
    private EditText rg_et_password;
    private EditText rg_et_aspassword;
    private Button rg_bt_regist;
    private String major;
    private String department;
    private String username;
    private String name;
    private String emli;
    private String plone;
    private String password;
    private String aspassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initEvent();
    }


    private void initView() {
        home_back = (ImageView) findViewById(R.id.home_back);
        home_title = (TextView) findViewById(R.id.home_title);
        home_title.setText("用户注册");
        home_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rg_et_username = (EditText) findViewById(R.id.rg_et_username);
        rg_et_name = (EditText) findViewById(R.id.rg_et_name);
        rg_et_emli = (EditText) findViewById(R.id.rg_et_emli);
        rg_et_plone = (EditText) findViewById(R.id.rg_et_plone);
        sp_department = (Spinner) findViewById(R.id.sp_department);
        sp_major = (Spinner) findViewById(R.id.sp_major);
        rg_et_password = (EditText) findViewById(R.id.rg_et_password);
        rg_et_aspassword = (EditText) findViewById(R.id.rg_et_aspassword);
        rg_bt_regist = (Button) findViewById(R.id.rg_bt_regist);

        rg_bt_regist.setOnClickListener(this);
    }


    private void initEvent() {

        sp_department.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] arr1 = getResources().getStringArray(R.array.demo1);
                department = arr1[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        sp_major.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] arr1 = getResources().getStringArray(R.array.demo2);
                major = arr1[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rg_bt_regist:
                submit();
                getData();
                break;
        }
    }

    private void getData() {

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.2.64.83:8080/app2/user/regist";
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("name",name);
            jsonObject.put("password",password);
            jsonObject.put("email",emli);
            jsonObject.put("telephone",plone);
            jsonObject.put("department",department);
            jsonObject.put("major",major);
            jsonObject.put("role","学生");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: "+jsonObject.toString());
                Gson gson = new Gson();
                MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);
                if (myBean.isSuccess()){
                    Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误"+volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }

    private boolean submit() {
        // validate
        username = rg_et_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return false;
        }

        name = rg_et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "真实姓名", Toast.LENGTH_SHORT).show();
            return false;
        }

        emli = rg_et_emli.getText().toString().trim();
        if (TextUtils.isEmpty(emli)) {
            Toast.makeText(this, "邮箱：用来找回密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        plone = rg_et_plone.getText().toString().trim();
        if (TextUtils.isEmpty(plone)) {
            Toast.makeText(this, "电话", Toast.LENGTH_SHORT).show();
            return false;
        }

        password = rg_et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        aspassword = rg_et_aspassword.getText().toString().trim();
        if (TextUtils.isEmpty(aspassword)) {
            Toast.makeText(this, "重复密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;

    }
}